// pages/search/search.js
let {
  getsearch
} = require("../../api/search")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    keyword: '',
    keylists: [],
    mylists: null,
    one: true,
    two: false,
    three: false,
    searchList: [
      "小蘑菇典藏版",
      "天官赐福漫画",
      "魔道祖师",
      "我行让我上",
      "木苏里《盛夏》",
      "明星大侦探",
      "LOFTER",
      "老福特",
    ]
  },
  find(e) {
    this.setData({
      keyword: e.detail.value
    })
  },
  okfind() {  
    wx.request({
      url: `https://www.lofter.com/market/search/product.json?keyword=${this.data.keyword}`,
      method: "GET",
      success: (data) => {
        if (!data) {
          this.data.one = false
          this.data.two = true
          this.data.three = false
          this.setData(this.data)
          return
        } else {
          this.data.one = false
          this.data.two = false
          this.data.three = true
        }
        if (!data.data.products.length) {
          this.data.one = false
          this.data.two = true
          this.data.three = false
          this.setData(this.data)
          return
        }

        let search = data.data.products
        this.data.mylists = search
        if (this.data.keyword != '') {
          this.data.keylists.push(this.data.keyword)
        } else {
          this.data.one = true
          this.data.two = false
          this.data.three = false
        }
        this.setData(this.data)
      }
    })

  },
  clear() {
    this.setData({
      keylists: []
    })
  },
  searchData(e) {
    let keyword =  e.target .dataset .keyword
    wx.request({
      url: `https://www.lofter.com/market/search/product.json?keyword=${keyword}`,
      method: "GET",
      success: (data) => {
        if (!data) {
          this.data.one = false
          this.data.two = true
          this.data.three = false
          this.setData(this.data)
          return
        } else {
          this.data.one = false
          this.data.two = false
          this.data.three = true
        }
        if (!data.data.products.length) {
          this.data.one = false
          this.data.two = true
          this.data.three = false
          this.setData(this.data)
          return
        }

        let search = data.data.products
        this.data.mylists = search
        if (this.data.keyword != '') {
          this.data.keylists.push(this.data.keyword)
        } else {
          this.data.one = false
          this.data.two = false
          this.data.three = true
        }
        this.setData(this.data)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})